const app = getApp()
import {
  parseTime
} from '../../utils/util.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    presentStageArr: [
      '待处理',
      '进行中',
      '已完成',
      '已逾期'
    ],
    isShow: true,
    orderData: [],
    pageData: {
      pageNo: 1,
      pageSize: 10
    },
    total:0,
    isLoading:false,
    queryFrom: {
      treatmentId: '',
      dateEnd: "",
      dateStart: "",
    },
    isLock:null,
    hospitalId:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.id);
    let that = this
    that.setData({
      hospitalId:options.id?options.id:''
    })
    // this.getList()
  },
  getList() {
    let that = this
    that.setData({
      isLoading:true,
    })
    wx.showLoading({
      title: '加载中...',
      mask:true
    })
    if(that.data.hospitalId){
      let queryFrom = that.data.queryFrom
      queryFrom.hospitalId = that.data.hospitalId
      queryFrom.transferDateStart = queryFrom.dateStart
      queryFrom.transferDateEnd = queryFrom.dateEnd
      wx.request({
        url: app.globalData.serverUrlgdb + '/ddOrder/page/' + that.data.pageData.pageNo + '/' + that.data.pageData.pageSize,
        method: 'POST',
        header: {
          'token': wx.getStorageSync('token')
        },
        data: queryFrom,
        success:(res=>{
          let resp = res.data
          if (resp.code == 200){
            resp.data.ddOrderPage.records.map(item=>{
              item.deviceReturnTime = parseTime(item.configurationTime, '{m}月{d}日')
              item.deviceReturnTimes = parseTime(item.configurationTime, '{y}-{m}-{d} {h}:{i}:{s}')
            })
            let arr = that.data.orderData.concat(resp.data.ddOrderPage.records)
            that.setData({
              orderData: arr,
              total:resp.data.ddOrderPage.total
            })
          }
        }),
        complete(){
          that.setData({
            isLoading:false
          })
        }
      })
      wx.hideLoading()
    }else{
      wx.request({
        url: app.globalData.serverUrlgdb + '/medical/order/page/' + that.data.pageData.pageNo + '/' + that.data.pageData.pageSize,
        method: 'POST',
        header: {
          'token': wx.getStorageSync('token')
        },
        data: that.data.queryFrom,
        success: (res => {
          let resp = res.data
          if (resp.code == 200) {
            resp.data.orderPage.records.map(item => {
              item.deviceReturnTime = parseTime(item.configurationTime, '{m}月{d}日')
              item.deviceReturnTimes = parseTime(item.configurationTime, '{y}-{m}-{d} {h}:{i}:{s}')
            })
            let arr = that.data.orderData.concat(resp.data.orderPage.records)
            that.setData({
              orderData: arr,
              total:resp.data.orderPage.total
            })
            wx.hideLoading()
          }
        }),
        complete(){
          that.setData({
            isLoading:false
          })
        }
      })
    }
    
  },
  bindSearchChange(e) {
    this.setData({
      'queryFrom.dateEnd': e.detail.endDate == '结束日期' ? '' : e.detail.endDate,
      'queryFrom.dateStart': e.detail.startDate == '开始日期' ? '' : e.detail.startDate,
      'queryFrom.treatmentId': e.detail.doctorsId,
      orderData:[],
      'pageData.pageNo':1
    })
    this.getList()
  },
  onShow() {
    this.setData({
      isLock:null
    })
    this.setData({
      'queryFrom.dateEnd': '',
      'queryFrom.dateStart': '',
      'queryFrom.treatmentId': '',
      'pageData.pageNo':1,
      orderData:[]
    })
    this.getList()
  },
  /**
   * 用户点击跳转产品详情
   */
  toDetail(e) {
    let that = this
    let lock=null
    if(!that.data.isLock){
      that.setData({
        isLock:new Date().getTime()
      })
    }else{
      lock = new Date().getTime()
    }
    if(that.data.isLock && lock){
      if(lock-that.data.isLock<app.globalData.isNumLock){
        return
      }else{
        that.setData({
          isLock:lock
        })
      }
    }
    wx.navigateTo({
      url: '/pagesB/orderdetail/orderdetail?id=' + e.currentTarget.dataset.id
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    // 1. 重置关键数据
    this.setData({
      'pageData.pageNo': 1,
      total: 0,
      isloading: false,
      orderData:[]
    })
    // 2. 重新发起请求 并关闭下拉窗口
    this.getList()
    wx.stopPullDownRefresh()
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    let that = this
    if(that.data.isLoading)return
    let pageNo =that.data.pageData.pageNo
    this.setData({
      'pageData.pageNo': pageNo += 1 // 让页码值自增 +1
    })
    if(that.data.orderData.length<that.data.total){
      that.getList() // 重新获取列表数据
    }else{
         wx.showToast({
           title: '暂无更多数据！',
           icon:'none'
         })
    }
  },
})